
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using Gman.Entity.Entities;
using Gman.Entity.Interfaces;
using Gman.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace Gman.Data
{
  //-------------------------------------------------------------------
  //Archivo     : PersonalDA.cs
  //Proyecto    : Gman
  //Creacion    : BIS(NombreDesarrollador) 20130226
  //Modificacion: 
  //Descripcion : Clase para capa de datos.
  //-------------------------------------------------------------------
  public class PersonalDA : IPersonal
  {

  Database db = DatabaseFactory.CreateDatabase();

  //-----------------------------------------------------------------------------------------------
  //Nombre: Listar
  //Objetivo: Retorna una lista de registros de tipo PersonalBE  
  //Creacion    : TISI(NombreDesarrollador) 20130226  
  //-----------------------------------------------------------------------------------------------
  public IList<PersonalBE> Listar(PersonalBE oItem)
  {
    IList<PersonalBE> lista = new List<PersonalBE>();			
    try
    {				
      DbCommand cmd = db.GetStoredProcCommand("SP_ListarPersonal");
      db.AddInParameter(cmd, "@IDEmp", DbType.Int32, oItem.IDEmp);
      db.AddInParameter(cmd, "@IDOrigen", DbType.Int64, oItem.IDOrigen);
      db.AddInParameter(cmd, "@OrigenPers", DbType.Int32, oItem.OrigenPers);

      using (IDataReader oR = db.ExecuteReader(cmd))
      {
        lista = MakeUniqueDatos(oR);
      }
      
    }catch (Exception ex){
      throw new Exception("PersonalDA.Listar()", ex);
    }
    finally
    {
    }
    return lista;
  }

  //-----------------------------------------------------------------------------------------------
  //Nombre: Obtener
  //Objetivo: Retorna un objeto de Tipo PersonalBE  
  //Creacion    : BIS(NombreDesarrollador) 20130226  
  //-----------------------------------------------------------------------------------------------
  public PersonalBE Obtener(PersonalBE oItem)
  {
    PersonalBE itemResult = null;
    try
    {
      DbCommand cmd = db.GetStoredProcCommand("SP_ObtenerPersonal");
      db.AddInParameter(cmd, "@NroDocuPers", DbType.String, oItem.NroDocuPers);
      db.AddInParameter(cmd, "@IDEmp", DbType.Int32, oItem.IDEmp);

      using (IDataReader oR = db.ExecuteReader(cmd))
      {
        while (oR.Read())
        {
          itemResult = MakeDatosMapeados(oR);
        }
      }
      return itemResult;
    }
    catch (Exception ex)
    {
      throw new Exception("PersonalDA.Obtener()", ex);                  
    }
    finally
    {
    }
  }

  //-----------------------------------------------------------------------------------------------
  //Nombre: Registrar
  //Objetivo: Almacena el registro de un objeto de tipo PersonalBE  
  //Creacion    : TISI(NombreDesarrollador) 20130226  
  //----------------------------------------------------------------------------------------------
  public int Registrar(PersonalBE oItem)
  { 
    int resultado = 0;
    try
    {
      DbCommand cmd = db.GetStoredProcCommand("SP_RegistrarPersonal");						
      //db.AddInParameter(cmd, "@IDPersonal", DbType.Int32 , oItem.IDPersonal);
      db.AddInParameter(cmd, "@IDOrigen", DbType.Int64 , oItem.IDOrigen);
      db.AddInParameter(cmd, "@NomsPers", DbType.String, oItem.NomsPers);
      db.AddInParameter(cmd, "@NroDocuPers", DbType.String, oItem.NroDocuPers);
      db.AddInParameter(cmd, "@TipoDocuPers", DbType.Int32 , oItem.TipoDocuPers);
      db.AddInParameter(cmd, "@CargoPers", DbType.String, oItem.CargoPers);
      db.AddInParameter(cmd, "@TipoPers", DbType.String, oItem.TipoPers);
      db.AddInParameter(cmd, "@JefeInmePers", DbType.String, oItem.JefeInmePers);
      db.AddInParameter(cmd, "@EmailPers", DbType.String, oItem.EmailPers);
      db.AddInParameter(cmd, "@NroTelfPers", DbType.String, oItem.NroTelfPers);
      db.AddInParameter(cmd, "@FecIngrPers", DbType.DateTime, oItem.FecIngrPers);
      db.AddInParameter(cmd, "@NroCeluPers1", DbType.String, oItem.NroCeluPers1);
      db.AddInParameter(cmd, "@NroCeluPers2", DbType.String, oItem.NroCeluPers2);
      db.AddInParameter(cmd, "@NroCeluPers3", DbType.String, oItem.NroCeluPers3);
      db.AddInParameter(cmd, "@NroCeluPers4", DbType.String, oItem.NroCeluPers4);
      db.AddInParameter(cmd, "@EstadoPers", DbType.Int32 , oItem.EstadoPers);
      db.AddInParameter(cmd, "@UsuReg", DbType.String, oItem.UsuReg);
      //db.AddInParameter(cmd, "@FecReg", DbType.DateTime, oItem.FecReg);
      db.AddInParameter(cmd, "@TermCnxReg", DbType.String, oItem.TermCnxReg);
      //db.AddInParameter(cmd, "@UsuMod", DbType.String, oItem.UsuMod);
      //db.AddInParameter(cmd, "@FecMod", DbType.String, oItem.FecMod);
      //db.AddInParameter(cmd, "@TermCnxMod", DbType.String, oItem.TermCnxMod);
      db.AddInParameter(cmd, "@IDEmp", DbType.Int32 , oItem.IDEmp);
      db.AddInParameter(cmd, "@OrigenPers", DbType.Int32, oItem.OrigenPers);
      db.AddInParameter(cmd, "@ApePers", DbType.String, oItem.ApePers);
      db.AddInParameter(cmd, "@AreaPers", DbType.String, oItem.AreaPers);
      
      resultado = db.ExecuteNonQuery(cmd);      

    }
    catch (Exception ex)
    {
      throw new Exception("PersonalDA.Registrar()", ex);
    }
    finally
    {

    }
    return resultado;
  }

  //-----------------------------------------------------------------------------------------------
  //Nombre: Actualizar
  //Objetivo: Actualiza el registro de un objeto de tipo PersonalBE 
  //Creacion    : TISI(NombreDesarrollador) 20130226  
  //-----------------------------------------------------------------------------------------------
  public int Actualizar(PersonalBE oItem)
  {
    int resultado = 0;
    try
    {
      DbCommand cmd = db.GetStoredProcCommand("SP_ActualizarPersonal");						
      db.AddInParameter(cmd, "@IDPersonal", DbType.Int32 , oItem.IDPersonal);
      db.AddInParameter(cmd, "@IDOrigen", DbType.Int64 , oItem.IDOrigen);
      db.AddInParameter(cmd, "@NomsPers", DbType.String, oItem.NomsPers);
      db.AddInParameter(cmd, "@NroDocuPers", DbType.String, oItem.NroDocuPers);
      db.AddInParameter(cmd, "@TipoDocuPers", DbType.Int32 , oItem.TipoDocuPers);
      db.AddInParameter(cmd, "@CargoPers", DbType.String, oItem.CargoPers);
      db.AddInParameter(cmd, "@TipoPers", DbType.String, oItem.TipoPers);
      db.AddInParameter(cmd, "@JefeInmePers", DbType.String, oItem.JefeInmePers);
      db.AddInParameter(cmd, "@EmailPers", DbType.String, oItem.EmailPers);
      db.AddInParameter(cmd, "@NroTelfPers", DbType.String, oItem.NroTelfPers);
      db.AddInParameter(cmd, "@FecIngrPers", DbType.DateTime, oItem.FecIngrPers);
      db.AddInParameter(cmd, "@NroCeluPers1", DbType.String, oItem.NroCeluPers1);
      db.AddInParameter(cmd, "@NroCeluPers2", DbType.String, oItem.NroCeluPers2);
      db.AddInParameter(cmd, "@NroCeluPers3", DbType.String, oItem.NroCeluPers3);
      db.AddInParameter(cmd, "@NroCeluPers4", DbType.String, oItem.NroCeluPers4);
      db.AddInParameter(cmd, "@EstadoPers", DbType.Int32 , oItem.EstadoPers);
      //db.AddInParameter(cmd, "@UsuReg", DbType.String, oItem.UsuReg);
      //db.AddInParameter(cmd, "@FecReg", DbType.DateTime, oItem.FecReg);
      //db.AddInParameter(cmd, "@TermCnxReg", DbType.String, oItem.TermCnxReg);
      db.AddInParameter(cmd, "@UsuMod", DbType.String, oItem.UsuMod);
      //db.AddInParameter(cmd, "@FecMod", DbType.String, oItem.FecMod);
      db.AddInParameter(cmd, "@TermCnxMod", DbType.String, oItem.TermCnxMod);
      //db.AddInParameter(cmd, "@IDEmp", DbType.Int32 , oItem.IDEmp);
      db.AddInParameter(cmd, "@ApePers", DbType.String, oItem.ApePers);
      db.AddInParameter(cmd, "@AreaPers", DbType.String, oItem.AreaPers);

      resultado = db.ExecuteNonQuery(cmd);

    }
    catch (Exception ex)
    {
      throw new Exception("PersonalDA.Actualizar()", ex);
    }
    finally
    {

    }
    return resultado;
  }

  //-----------------------------------------------------------------------------------------------
  //Nombre: Eliminar
  //Objetivo: Elimina el registro de un objeto de tipo PersonalBE  
  //Creacion    : BIS(NombreDesarrollador) 20130226  
  //-----------------------------------------------------------------------------------------------
  public int Eliminar(PersonalBE oItem)
  {
    int resultado = 0;
    try
    {
      DbCommand cmd = db.GetStoredProcCommand("SP_EliminarPersonal");						
      db.AddInParameter(cmd, "@IDPersonal", DbType.Int32 , oItem.IDPersonal);
      db.AddInParameter(cmd, "@UsuMod", DbType.String, oItem.UsuMod);
      db.AddInParameter(cmd, "@TermCnxMod", DbType.String, oItem.TermCnxMod);
      resultado = db.ExecuteNonQuery(cmd);      

    }
    catch (Exception ex)
    {
      throw new Exception("PersonalDA.Eliminar()", ex);
  }
  finally
  {

  }
  return resultado;
  }  

  private IList<PersonalBE> MakeUniqueDatos(IDataReader oR)
  {
    IList<PersonalBE> list = new List<PersonalBE>();

    while (oR.Read())
      list.Add(MakeDatosMapeados(oR));

    return list;
  }

  private PersonalBE MakeDatosMapeados(IDataReader oR)
  {
    var itemResult = new PersonalBE();
    
      itemResult.IDPersonal= oR["IDPersonal"].ToInt32();
      itemResult.IDOrigen= oR["IDOrigen"].ToInt64();
      itemResult.NomsPers= oR["NomsPers"].ToString();
      itemResult.NroDocuPers= oR["NroDocuPers"].ToString();
      itemResult.TipoDocuPers= oR["TipoDocuPers"].ToInt32();
      itemResult.CargoPers= oR["CargoPers"].ToString();
      itemResult.TipoPers= oR["TipoPers"].ToString();
      itemResult.JefeInmePers= oR["JefeInmePers"].ToString();
      itemResult.EmailPers= oR["EmailPers"].ToString();
      itemResult.NroTelfPers= oR["NroTelfPers"].ToString();
      itemResult.FecIngrPers= oR["FecIngrPers"].ToDateTime();
      itemResult.NroCeluPers1= oR["NroCeluPers1"].ToString();
      itemResult.NroCeluPers2= oR["NroCeluPers2"].ToString();
      itemResult.NroCeluPers3= oR["NroCeluPers3"].ToString();
      itemResult.NroCeluPers4= oR["NroCeluPers4"].ToString();
      itemResult.EstadoPers= oR["EstadoPers"].ToInt32();
      itemResult.UsuReg= oR["UsuReg"].ToString();
      itemResult.FecReg= oR["FecReg"].ToDateTime();
      itemResult.TermCnxReg= oR["TermCnxReg"].ToString();
      itemResult.UsuMod= oR["UsuMod"].ToString();
      itemResult.FecMod= oR["FecMod"].ToString();
      itemResult.TermCnxMod= oR["TermCnxMod"].ToString();
      itemResult.IDEmp= oR["IDEmp"].ToInt32();
      itemResult.ApePers = oR["ApePers"].ToString();
      itemResult.AreaPers = oR["AreaPers"].ToString();
    return itemResult;
  }
 }
}
